package com.xwaf.platform.web.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.entity.QuartzEntity;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.service.JobService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
@Api(tags = "JOB自定义定时任务", hidden = true)
@RequestMapping(value = "/x/job")
public class JobController extends BaseController {

	private final static Logger LOGGER = LoggerFactory.getLogger(JobController.class);

	@Autowired
	private JobService jobService;

	@ApiOperation(value = "分页查询", response = QuartzEntity.class)
	@PostMapping(value = "page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody QuartzEntity project) {
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<QuartzEntity> page = new Page<QuartzEntity>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<QuartzEntity> result = jobService.selPageList(page, project);
		return CommonResponse.createCommonResponse(result);
	}

	/**
	 * 
	 * <p>
	 * http://localhost:19102/x/job/add?<br>
	 * jobClassName=com.xwaf.platform.system.provider.task.AddJob<br>
	 * description=测试任务1&jobGroup=test&jobName=test02<br>
	 * &cronExpression=0/10 * * * * ?
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:32:51 <br>
	 * @see 无<br>
	 * @param quartz
	 * @return
	 * @demo <br>
	 */
	@PostMapping("/add")
	public CommonResponse save(QuartzEntity quartz) {
		LOGGER.info("新增任务");
		jobService.add(quartz);
		return CommonResponse.createCommonResponse();
	}

	// http://localhost:19102/x/job/pause?jobName=test01&jobGroup=test
	@PostMapping("/pause")
	public CommonResponse pause(QuartzEntity quartz, HttpServletResponse response) {
		LOGGER.info("停止任务");
		jobService.pause(quartz);
		return CommonResponse.createCommonResponse();
	}

	@PostMapping("/resume")
	public CommonResponse resume(QuartzEntity quartz, HttpServletResponse response) {
		LOGGER.info("恢复任务");
		jobService.resume(quartz);
		return CommonResponse.createCommonResponse();
	}

	@PostMapping("/remove")
	public CommonResponse remove(QuartzEntity quartz, HttpServletResponse response) {
		LOGGER.info("移除任务");
		jobService.remove(quartz);
		return CommonResponse.createCommonResponse();
	}
}
